package com.jilintongyi.jlcrtvu.util;

import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 用户工具类
 */
public class UserUtils {
	/**
	 * 获取当前用户名
	 * 
	 * @return
	 */
	public static String getCurrentUsername() {
		return getCurrentAuthentication().getName();
	}

	/**
	 * 获取当前身份认证
	 * 
	 * @return
	 */
	public static Authentication getCurrentAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取当前请求的权限信息
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<SimpleGrantedAuthority> getCurrentAuthorities() {
		return (List<SimpleGrantedAuthority>) getCurrentAuthentication().getAuthorities();
	}

	/**
	 * 判断权限
	 * 
	 * @param authority
	 * @return
	 */
	public static Boolean hasAuthority(String authority) {
		Boolean hasAuthority = false;
		List<SimpleGrantedAuthority> list = getCurrentAuthorities();
		for (SimpleGrantedAuthority s : list) {
			if (authority.equals(s.getAuthority())) {
				hasAuthority = true;
				break;
			}
		}
		return hasAuthority;
	}
}
